<template>
  <el-dialog
    :title="!dataForm.code ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    
    <el-form-item label="设备类型" prop="categoryName">
      <el-select v-model="dataForm.categoryName" placeholder="请选择设备类型" @change="setCategoryId(dataForm.categoryName)">
        <el-option
          v-for="item in categoryList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
        
    <el-form-item label="报警类型" prop="alarmTypeName">
      <el-select v-model="dataForm.alarmTypeName" placeholder="请选择设备报警类型" @change="setAlarmTypeId(dataForm.alarmTypeName)">
        <el-option
          v-for="item in alarmTypeList"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    
    <el-form-item label="设置上限" prop="maxValue">
      <el-input v-model="dataForm.maxValue" placeholder="上限值"></el-input>
    </el-form-item>
    <el-form-item label="设置下限" prop="minValue">
      <el-input v-model="dataForm.minValue" placeholder="下限值"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        loading: false,
        dataForm: {
          id: '',
          remark: '',
          alarmTypeId: '',
          categoryId: '',
          maxValue: '',
          minValue: '',
          alarmTypeName: '',
          categoryName: ''
        },
        categoryList: [],
        alarmTypeList: [],
        dataRule: {
          alarmTypeName: [
            { required: true, message: '报警类型不能为空', trigger: 'blur' }
          ],
          categoryName: [
            { required: true, message: '设备类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/threshold/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data) {
                this.dataForm.id = data.tDevice.id
                this.dataForm.alarmTypeId = data.tDevice.alarmTypeId
                this.dataForm.categoryId = data.tDevice.deviceCategoryId
                this.dataForm.countryName = data.tDevice.countryName
                this.dataForm.categoryName = data.tDevice.categoryName
                this.dataForm.remark = data.tDevice.remark
                this.dataForm.maxValue = data.tDevice.maxValue
                this.dataForm.minValue = data.tDevice.minValue
              }
            })
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/devices/alarmType`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.alarmTypeList = data.data.map(item => {
              return {value: `${item.id}`, label: `${item.name}`}
            })
          }
        })
        this.$http({
          url: this.$http.adornUrl(`/devices/category`),
          method: 'get'
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.categoryList = data.data.map(item => {
              return {value: `${item.id}`, label: `${item.name}`}
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/threshold/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id,
                'remark': this.dataForm.remark,
                'alarmTypeId': this.dataForm.alarmTypeId,
                'deviceCategoryId': this.dataForm.categoryId,
                'maxValue': this.dataForm.maxValue,
                'minValue': this.dataForm.minValue,
                'createAt': this.dataForm.createAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      setAlarmTypeId (countryId) {
        console.log('alarmTypeId=' + countryId)
        this.dataForm.alarmTypeId = countryId
      },
      setCategoryId (categoryId) {
        console.log('categoryId=' + categoryId)
        this.dataForm.categoryId = categoryId
      }
    }
  }
</script>
